﻿Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.OleDb
Imports System.Text
Imports System.Threading
Imports FL_8x0.Base
Imports FL_8x0.Common
Imports FL_8x0.Common.SystemData
Imports GalaSoft.MvvmLight.Messaging

Namespace FL_8x0.FL_800.Model

    Public Class GroupModel
        Inherits ProductModel

        Private strTime800() = {"时间_延时", "时间_充气1", "时间_充气2", "时间_平衡1", "时间_平衡2", "时间_检测", "时间_排气", "时间_基准检测", "时间_等待排气"}
        Private strLimit800() = {"2NG", "NG上限", "NG下限", "MNG上限", "MNG下限", "前导压标准", "前导压上限", "前导压下限", "测试压标准", "测试压上限", "测试压下限"}

        Private strTime800MS() = {"时间_延时", "时间_充气", "时间_平衡", "时间_小漏检测", "时间_大漏检测", "时间_排气", "时间_基准检测"}
        Private strLimit800MS() = {"SNG上限", "SNG下限", "LNG上限", "LNG下限", "测试压标准", "测试压上限", "测试压下限"}

        Public Sub New()
            _times = New ObservableCollection(Of String)
            For i = 0 To 8
                _times.Add("")
            Next

            _limits = New ObservableCollection(Of String)
            For i = 0 To 10
                _limits.Add("")
            Next

            _sv5List = New ObservableCollection(Of Boolean)
            _sv6List = New ObservableCollection(Of Boolean)
            _sv7List = New ObservableCollection(Of Boolean)
            _sv8List = New ObservableCollection(Of Boolean)

            For i = 0 To 7
                _sv5List.Add(False)
                _sv6List.Add(False)
                _sv7List.Add(False)
                _sv8List.Add(False)
            Next
        End Sub

        Private _isMs As Boolean


        Public Function BuildGroupCmd() As String
            Dim sb = New StringBuilder()
            sb.Append(ChrW(2))
            sb.Append(Format(UShort.Parse(GroupId), "00"))

            For i = 0 To 7
                sb.Append(FillInSpaces(Format(Double.Parse(Times(i)), TimerDigit), 5))
            Next

            For i = 0 To 4
                sb.Append(FillInSpaces(Format(Double.Parse(Limits(i)), LeakDigit), 6))
            Next

            For i = 5 To 10
                If (Limits(i).Contains(".")) Then
                    sb.Append(FillInSpaces(Format(Double.Parse(Limits(i)), "000.0"), 6))
                ElseIf (Integer.Parse(Limits(i)) < 9999) Then
                    sb.Append(FillInSpaces(Format(Integer.Parse(Limits(i)), "0000"), 6))
                Else
                    sb.Append(FillInSpaces(Format(Integer.Parse(Limits(i)), "00000"), 6))
                End If
            Next

            sb.Append(Format(Double.Parse(WValoume), "+0.0000E+0"))
            sb.Append(Format(Double.Parse(MValoume), "+0.0000E+0"))
            sb.Append(LeakUnit)
            sb.Append(PressureUnit)
            sb.Append(LeakDigit)

            sb.Append(TimerDigit)
            sb.Append(UsbMode)
            sb.Append(RS232Mode)
            sb.Append(Cylinder)
            For Each it In Sv5List
                If it Then
                    sb.Append("1")
                Else
                    sb.Append("0")
                End If
            Next
            For Each it In Sv6List
                If it Then
                    sb.Append("1")
                Else
                    sb.Append("0")
                End If
            Next
            For Each it In Sv7List
                If it Then
                    sb.Append("1")
                Else
                    sb.Append("0")
                End If
            Next
            For Each it In Sv8List
                If it Then
                    sb.Append("1")
                Else
                    sb.Append("0")
                End If
            Next

            sb.Append(MasterMode)

            If Not IsMs AndAlso SystemData.Version <> Version_800_113 Then
                sb.Append(IIf((MiddleJudgment), "1", "0"))
                sb.Append(FillInSpaces(LeadPDAC, 5))
                sb.Append(FillInSpaces(TestPDAC, 5))
            End If

            sb.Append(ChrW(3))

            Dim res = sb.ToString

            Dim parity = BCC(res)

            Return Chr(6) & res & Chr(parity)

        End Function

        Friend Function Check() As Boolean
            If _isMs Then
                Return Check800MS()
            Else
                Return Check800()
            End If
        End Function

        Private Function Check800() As Boolean

            '校验时间参数
            For i = 0 To 8
                If TimerDigit = " 0000" Then
                    If i = 0 Or i = 1 Or i = 3 Or i = 8 Then
                        If Not CheckTextNumber(strTime800(i), Times(i), 9999, 0) Then
                            Return False
                        End If
                    ElseIf MasterMode <> 4 And i = 7 Then
                        If Not CheckTextNumber(strTime800(i), Times(i), 150, 10) Then
                            Return False
                        End If
                    Else
                        If Not CheckTextNumber(strTime800(i), Times(i), 9999, 1) Then
                            Return False
                        End If
                    End If
                End If

                If TimerDigit = "000.0" Then
                    If i = 0 Or i = 1 Or i = 3 Or i = 8 Then
                        If Not CheckTextNumber(strTime800(i), Times(i), 999.9, 0) Then
                            Return False
                        End If
                    ElseIf MasterMode <> 4 And i = 7 Then
                        If Not CheckTextNumber(strTime800(i), Times(i), 150, 10) Then
                            Return False
                        End If
                    Else
                        If Not CheckTextNumber(strTime800(i), Times(i), 999.9, 0.1) Then
                            Return False
                        End If
                    End If
                End If
            Next


            '校验泄漏NG判定参数
            For i = 0 To 4
                If i = 2 Or i = 4 Then
                    If Not CheckTextNumber(strLimit800(i), Limits(i), 0, -9999) Then
                        Return False
                    End If
                Else
                    If Not CheckTextNumber(strLimit800(i), Limits(i), 9999, 0) Then
                        Return False
                    End If
                End If
            Next

            '校验压力参数
            For i = 5 To 10
                If Not CheckTextNumber(strLimit800(i), Limits(i), 999.9, -999.9) Then
                    Return False
                End If
            Next

            '校验前导压压力判定参数
            '上限 小于 标准
            If Limits(6) < Limits(5) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(6) & "]必须大于[" & strLimit800(5) & "]")
                Return False
            End If
            '下限 大于 标准
            If Limits(7) > Limits(5) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(7) & "]必须小于[" & strLimit800(5) & "]")
                Return False
            End If
            '下限 大于 上限
            If Limits(7) > Limits(6) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(7) & "]必须小于[" & strLimit800(6) & "]")
                Return False
            End If

            '校验测试压力判定参数
            '上限 小于 标准
            If Limits(9) < Limits(8) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(9) & "]必须大于[" & strLimit800(8) & "]")
                Return False
            End If
            '下限 大于 标准
            If Limits(10) > Limits(8) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(10) & "]必须小于[" & strLimit800(8) & "]")
                Return False
            End If
            '下限 大于上限
            If Limits(10) > Limits(9) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(10) & "]必须小于[" & strLimit800(9) & "]")
                Return False
            End If

            If Not CheckTextNumber("被测物容积", WValoume, 9900000000.0, 4.5) Then
                Return False
            End If
            If Not CheckTextNumber("基准物容积", MValoume, 9900000000.0, 4.5) Then
                Return False
            End If

            Return True
        End Function

        Private Function Check800MS() As Boolean
            '校验时间参数
            For i = 0 To 5
                If TimerDigit = " 0000" Then
                    If i = 0 Then
                        If Not CheckTextNumber(strTime800MS(i), Times(i), 9999, 0) Then
                            Return False
                        End If
                    Else
                        If Not CheckTextNumber(strTime800MS(i), Times(i), 9999, 1) Then
                            Return False
                        End If
                    End If
                End If

                If TimerDigit = "000.0" Then
                    If i = 0 Then
                        If Not CheckTextNumber(strTime800MS(i), Times(i), 999.9, 0) Then
                            Return False
                        End If
                    Else
                        If Not CheckTextNumber(strTime800MS(i), Times(i), 999.9, 0.1) Then
                            Return False
                        End If
                    End If
                End If
            Next


            '校验泄漏NG判定参数
            For i = 0 To 3
                If i = 1 Or i = 3 Then
                    If Not CheckTextNumber(strLimit800MS(i), Limits(i), 0, -9999) Then
                        Return False
                    End If
                Else
                    If Not CheckTextNumber(strLimit800MS(i), Limits(i), 9999, 0) Then
                        Return False
                    End If
                End If
            Next

            '校验压力参数
            For i = 8 To 10
                If Not CheckTextNumber(strLimit800(i), Limits(i), 999.9, -999.9) Then
                    Return False
                End If
            Next


            '校验测试压力判定参数
            '上限 小于 标准
            If Limits(9) < Limits(8) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(9) & "]必须大于[" & strLimit800(8) & "]")
                Return False
            End If
            '下限 大于 标准
            If Limits(10) > Limits(8) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(10) & "]必须小于[" & strLimit800(8) & "]")
                Return False
            End If
            '下限 大于上限
            If Limits(10) > Limits(9) Then
                log.WriteLogError("输入参数检查", "[" & strLimit800(10) & "]必须小于[" & strLimit800(9) & "]")
                Return False
            End If
            Return True
        End Function

        Public Overrides Function WriteProduct(dbCon As OleDbConnection, reason As String, blnNeedWrite As Boolean) As Boolean
            Dim preProduct As GroupModel
            preProduct = ReadProduct(dbCon, Id)
            If Not preProduct.Id = "" Then
                Return EditProduct(preProduct, dbCon, reason, blnNeedWrite)
            Else
                Return AddProduct(dbCon, reason)
            End If
        End Function

        Public Overrides Function ReadProduct(dbCon As OleDbConnection, id As String) As ProductModel
            Dim udtProduct As New GroupModel With {
                .Id = ""
            }

            If id = "" Then
                log.WriteLogAssert("读取产品", "产品号=空")
                Return udtProduct
            End If

            If Not IsNothing(dbCon) Then
                Dim strCmd As String = "SELECT * FROM 产品管理 WHERE 产品号 = '" & id & "'"

                Dim dbAdapter As New OleDbDataAdapter
                Dim dbTable As New DataTable

                Try
                    dbAdapter = New OleDbDataAdapter(strCmd, dbCon) '打开数据库

                    dbAdapter.Fill(dbTable) '提取数据

                    If dbTable.Rows.Count > 0 Then
                        If dbTable.Rows.Item(0).Item("产品号") = id Then
                            udtProduct.Id = dbTable.Rows.Item(0).Item("产品号")
                            udtProduct.Name = dbTable.Rows.Item(0).Item("产品名称")
                            udtProduct.Model = dbTable.Rows.Item(0).Item("产品型号")
                            udtProduct.GroupId = dbTable.Rows.Item(0).Item("组号")

                            If Not IsMs Then
                                For i = 0 To udtProduct.Times.Count - 1
                                    udtProduct.Times(i) = dbTable.Rows.Item(0).Item(4 + i)
                                Next


                                For i = 0 To udtProduct.Limits.Count - 1
                                    udtProduct.Limits(i) = dbTable.Rows.Item(0).Item(14 + i)
                                Next
                                udtProduct.WValoume = dbTable.Rows.Item(0).Item("被测物容积(mL)")
                                udtProduct.MValoume = dbTable.Rows.Item(0).Item("基准物容积(mL)")
                                udtProduct.MiddleJudgment = dbTable.Rows.Item(0).Item("中途判定模式")
                                udtProduct.LeadPDAC = dbTable.Rows.Item(0).Item("前导压DAC")
                                udtProduct.TestPDAC = dbTable.Rows.Item(0).Item("测试压DAC")
                            Else
                                For i = 0 To 5
                                    udtProduct.Times(i) = dbTable.Rows.Item(0).Item(4 + i)
                                Next

                                For i = 0 To 3
                                    udtProduct.Limits(i) = dbTable.Rows.Item(0).Item(11 + i)
                                Next

                                For i = 0 To 2
                                    udtProduct.Limits(i + 8) = dbTable.Rows.Item(0).Item(14 + i)
                                Next
                            End If

                            udtProduct.TimerDigit = dbTable.Rows.Item(0).Item("时间感度")
                            udtProduct.LeakDigit = dbTable.Rows.Item(0).Item("泄漏量感度")
                            udtProduct.PressureUnit = dbTable.Rows.Item(0).Item("压力单位")
                            udtProduct.LeakUnit = dbTable.Rows.Item(0).Item("泄漏量单位")
                            udtProduct.MasterMode = dbTable.Rows.Item(0).Item("Master模式")
                            udtProduct.UsbMode = dbTable.Rows.Item(0).Item("输出模式")
                            udtProduct.RS232Mode = dbTable.Rows.Item(0).Item("串口输出模式")
                            udtProduct.Cylinder = dbTable.Rows.Item(0).Item("气缸控制模式")
                            udtProduct.SV5Mode = dbTable.Rows.Item(0).Item("SV5Mode")
                            udtProduct.Sv6Mode = dbTable.Rows.Item(0).Item("SV6Mode")
                            udtProduct.SV7Mode = dbTable.Rows.Item(0).Item("SV7Mode")
                            udtProduct.SV8Mode = dbTable.Rows.Item(0).Item("SV8Mode")
                            udtProduct.Temporary = dbTable.Rows.Item(0).Item("临时存储")
                        End If
                    End If
                Catch ex As Exception
                    log.WriteLogDebug("读取产品:" & id, ex.Message)
                Finally

                    dbAdapter.Dispose()
                    dbTable = Nothing
                    dbAdapter = Nothing
                End Try
            End If

            Return udtProduct
        End Function

        Private Function AddProduct(dbCon As OleDbConnection, reason As String) As Boolean
            If Id = "" Then
                log.WriteLogAssert("添加产品", "产品号=空")
                Return False
            End If
            Dim sbSQL = New StringBuilder
            Dim sbLog = New StringBuilder
            If IsMs Then
                sbSQL.Append("INSERT INTO 产品管理 ( ")
                sbSQL.Append("[产品号],")
                sbSQL.Append("[产品名称],")
                sbSQL.Append("[产品型号],")
                sbSQL.Append("[组号],")
                sbSQL.Append("[时间_延时],")
                sbSQL.Append("[时间_充气],")
                sbSQL.Append("[时间_平衡],")
                sbSQL.Append("[时间_小漏检测],")
                sbSQL.Append("[时间_大漏检测],")
                sbSQL.Append("[时间_排气],")
                sbSQL.Append("[时间感度],")


                sbSQL.Append("[SNG上限],")
                sbSQL.Append("[SNG下限],")
                sbSQL.Append("[LNG上限],")
                sbSQL.Append("[LNG下限],")

                sbSQL.Append("[测试压标准],")
                sbSQL.Append("[测试压上限],")
                sbSQL.Append("[测试压下限],")


                sbSQL.Append("[泄漏量单位],")
                sbSQL.Append("[压力单位],")
                sbSQL.Append("[泄漏量感度],")


                sbSQL.Append("[Master模式],")
                sbSQL.Append("[输出模式],")
                sbSQL.Append("[串口输出模式],")
                sbSQL.Append("[气缸控制模式],")
                sbSQL.Append("[SV5Mode],")
                sbSQL.Append("[SV6Mode],")
                sbSQL.Append("[SV7Mode],")
                sbSQL.Append("[SV8Mode],")
                sbSQL.Append("[临时存储]")


                sbSQL.Append(" ) VALUES ( ")

                sbSQL.Append("'" & Id & "',")
                sbSQL.Append("'" & Name & "',")
                sbSQL.Append("'" & Model & "',")
                sbSQL.Append(GroupId & ",")
                For i = 0 To 5
                    sbSQL.Append(Times(i) & ",")
                Next

                sbSQL.Append("'" & TimerDigit & "',")

                For i = 0 To 3
                    sbSQL.Append(Limits(i) & ",")
                Next

                For i = 8 To 10
                    sbSQL.Append(Limits(i) & ",")
                Next

                sbSQL.Append("'" & LeakUnit & "',")
                sbSQL.Append("'" & PressureUnit & "',")
                sbSQL.Append("'" & LeakDigit & "',")

                sbSQL.Append(MasterMode & ",")
                sbSQL.Append(UsbMode & ",")
                sbSQL.Append(RS232Mode & ",")
                sbSQL.Append(Cylinder & ",")
                sbSQL.Append("'" & SV5Mode & "',")
                sbSQL.Append("'" & Sv6Mode & "',")
                sbSQL.Append("'" & SV7Mode & "',")
                sbSQL.Append("'" & SV8Mode & "',")
                sbSQL.Append("" & Temporary & ")")


                sbLog.Append("{")
                sbLog.Append("产品号=" & Id & ",")
                sbLog.Append("产品名称=" & Name & ",")
                sbLog.Append("产品型号=" & Model & ",")
                sbLog.Append("组号=" & GroupId & ",")

                For i = 0 To 5
                    sbLog.Append(strTime800MS(i) & "=" & Times(i) & ",")
                Next

                sbLog.Append("时间感度=" & TimerDigit & ",")

                For i = 0 To 3
                    sbLog.Append(strLimit800MS(i) & "=" & Limits(i) & ",")
                Next

                For i = 8 To 10
                    sbLog.Append(strLimit800(i) & "=" & Limits(i) & ",")
                Next

                sbLog.Append("泄漏量单位=" & LeakUnit & ",")
                sbLog.Append("压力单位=" & PressureUnit & ",")
                sbLog.Append("泄漏量感度=" & LeakDigit & ",")

                sbLog.Append("Master模式=" & MasterMode & ",")
                sbLog.Append("输出模式=" & UsbMode & ",")
                sbLog.Append("串口输出模式=" & RS232Mode & ",")
                sbLog.Append("气缸控制模式=" & Cylinder & ",")
                sbLog.Append("SV5Mode=" & SV5Mode & ",")
                sbLog.Append("SV6Mode=" & Sv6Mode & ",")
                sbLog.Append("SV7Mode=" & SV7Mode & ",")
                sbLog.Append("SV8Mode=" & SV8Mode & "}")
            Else
                sbSQL.Append("INSERT INTO 产品管理 ( ")
                sbSQL.Append("[产品号],")
                sbSQL.Append("[产品名称],")
                sbSQL.Append("[产品型号],")
                sbSQL.Append("[组号],")
                sbSQL.Append("[时间_延时],")
                sbSQL.Append("[时间_充气1],")
                sbSQL.Append("[时间_充气2],")
                sbSQL.Append("[时间_平衡1],")
                sbSQL.Append("[时间_平衡2],")
                sbSQL.Append("[时间_检测],")
                sbSQL.Append("[时间_排气],")
                sbSQL.Append("[时间_基准检测],")
                sbSQL.Append("[时间_等待排气],")
                sbSQL.Append("[时间感度],")

                sbSQL.Append("[2NG],")
                sbSQL.Append("[NG上限],")
                sbSQL.Append("[NG下限],")
                sbSQL.Append("[MNG上限],")
                sbSQL.Append("[MNG下限],")

                sbSQL.Append("[前导压标准],")
                sbSQL.Append("[前导压上限],")
                sbSQL.Append("[前导压下限],")
                sbSQL.Append("[测试压标准],")
                sbSQL.Append("[测试压上限],")
                sbSQL.Append("[测试压下限],")


                sbSQL.Append("[泄漏量单位],")
                sbSQL.Append("[压力单位],")
                sbSQL.Append("[泄漏量感度],")

                sbSQL.Append("[被测物容积(mL)],")
                sbSQL.Append("[基准物容积(mL)],")
                sbSQL.Append("[Master模式],")
                sbSQL.Append("[输出模式],")
                sbSQL.Append("[串口输出模式],")
                sbSQL.Append("[气缸控制模式],")
                sbSQL.Append("[SV5Mode],")
                sbSQL.Append("[SV6Mode],")
                sbSQL.Append("[SV7Mode],")
                sbSQL.Append("[SV8Mode],")

                sbSQL.Append("[中途判定模式],")
                sbSQL.Append("[前导压DAC],")
                sbSQL.Append("[测试压DAC],")
                sbSQL.Append("[临时存储]")

                sbSQL.Append(" )")
                sbSQL.Append(" VALUES ( ")

                sbSQL.Append("'" & Id & "',")
                sbSQL.Append("'" & Name & "',")
                sbSQL.Append("'" & Model & "',")
                sbSQL.Append(GroupId & ",")
                For i = 0 To Times.Count - 1
                    sbSQL.Append(Times(i) & ",")
                Next
                sbSQL.Append("'" & TimerDigit & "',")

                For i = 0 To Limits.Count - 1
                    sbSQL.Append(Limits(i) & ",")
                Next


                sbSQL.Append("'" & LeakUnit & "',")
                sbSQL.Append("'" & PressureUnit & "',")
                sbSQL.Append("'" & LeakDigit & "',")

                sbSQL.Append(WValoume & ",")
                sbSQL.Append(MValoume & ",")
                sbSQL.Append(MasterMode & ",")
                sbSQL.Append(UsbMode & ",")
                sbSQL.Append(RS232Mode & ",")
                sbSQL.Append(Cylinder & ",")
                sbSQL.Append("'" & SV5Mode & "',")
                sbSQL.Append("'" & Sv6Mode & "',")
                sbSQL.Append("'" & SV7Mode & "',")
                sbSQL.Append("'" & SV8Mode & "',")

                sbSQL.Append(MiddleJudgment & ",")
                sbSQL.Append(LeadPDAC & ",")
                sbSQL.Append(TestPDAC & ",")
                sbSQL.Append(Temporary & ")")


                sbLog.Append("{")
                sbLog.Append("产品号=" & Id & ",")
                sbLog.Append("产品名称=" & Name & ",")
                sbLog.Append("产品型号=" & Model & ",")
                sbLog.Append("组号=" & GroupId & ",")

                For i = 0 To Times.Count - 1
                    sbLog.Append(strTime800(i) & "=" & Times(i) & ",")
                Next
                sbLog.Append("时间感度=" & TimerDigit & ",")


                For i = 0 To Limits.Count - 1
                    sbLog.Append(strLimit800(i) & "=" & Limits(i) & ",")
                Next


                sbLog.Append("泄漏量单位=" & LeakUnit & ",")
                sbLog.Append("压力单位=" & PressureUnit & ",")
                sbLog.Append("泄漏量感度=" & LeakDigit & ",")

                sbLog.Append("被测物容积(mL)=" & WValoume & ",")
                sbLog.Append("基准物容积(mL)=" & MValoume & ",")
                sbLog.Append("Master模式=" & MasterMode & ",")
                sbLog.Append("输出模式=" & UsbMode & ",")
                sbLog.Append("串口输出模式=" & RS232Mode & ",")
                sbLog.Append("气缸控制模式=" & Cylinder & ",")
                sbLog.Append("SV5Mode=" & SV5Mode & ",")
                sbLog.Append("SV6Mode=" & Sv6Mode & ",")
                sbLog.Append("SV7Mode=" & SV7Mode & ",")
                sbLog.Append("SV8Mode=" & SV8Mode & ",")

                sbLog.Append("中途判定模式=" & MiddleJudgment & ",")
                sbLog.Append("前导压DAC=" & LeadPDAC & ",")
                sbLog.Append("测试压DAC=" & TestPDAC & ",")
                sbLog.Append("临时存储=" & Temporary & "}")

            End If

            log.WriteLogInfo("添加产品", "{" & "添加原因=" & reason & ", " & sbLog.ToString & "}")

            Dim dbCmd As New OleDbCommand(sbSQL.ToString, dbCon)

            Try
                dbCmd.ExecuteNonQuery()
            Catch ex As Exception
                log.WriteLogError("添加产品:", "{" & "添加失败原因=" & ex.Message & "SQL:" & sbSQL.ToString & "," & sbLog.ToString & "}")
                Return False
            End Try

            Return True
        End Function

        Private Function EditProduct(preProduct As GroupModel, dbCon As OleDbConnection,
                                     reason As String, blnNeedWrite As Boolean) As Boolean
            If Id = "" Then
                log.WriteLogAssert("编辑产品", "产品号=空")
                Return False
            End If

            Dim shouldWrite As Boolean = blnNeedWrite
            Dim shouldNotify = False

            Dim sbSQL = New StringBuilder
            Dim sbLog = New StringBuilder

            sbLog.Append("{修改原因=")
            sbLog.Append(reason)
            sbLog.Append(",产品号=")
            sbLog.Append(Id)
            If preProduct.Name <> Name Then
                sbSQL.Append("产品名称='" & Name & "',") '文本
                sbLog.Append("{产品名称=" & preProduct.Name & "}变为{产品名称=" & Name & "},")
            End If

            If preProduct.Model <> Model Then
                sbSQL.Append("产品型号='" & Name & "',") '文本
                sbLog.Append("{产品型号=" & preProduct.Name & "}变为{产品型号=" & Name & "},")
            End If

            Dim TOLERANCE As Single = 0.000001

            If Not IsMs Then
                For i = 0 To preProduct.Times.Count - 1
                    If Math.Abs(Double.Parse(preProduct.Times(i)) - Double.Parse(Times(i))) > TOLERANCE Then
                        sbSQL.Append(strTime800(i) & "=" & Times(i) & ",")
                        sbLog.Append("{" & strTime800(i) & "=" & preProduct.Times(i) & "}变为{" &
                                   strTime800(i) & "=" & Times(i) & "},")
                        ' TODO 提醒写入
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                Next

                For i = 0 To preProduct.Limits.Count - 1
                    If Math.Abs(Double.Parse(preProduct.Limits(i)) - Double.Parse(Limits(i))) > TOLERANCE Then
                        sbSQL.Append(strLimit800(i) & "=" & Limits(i) & ",")
                        sbLog.Append("{" & strLimit800(i) & "=" & preProduct.Limits(i) & "}变为{" &
                                   strLimit800(i) & "=" & Limits(i) & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                Next

                If Double.Parse(preProduct.WValoume) <> Double.Parse(WValoume) Then
                    sbSQL.Append("[被测物容积(mL)]=" & Double.Parse(WValoume) & ",") '文本
                    sbLog.Append("{被测物容积(mL)=" & preProduct.WValoume & "}变为{被测物容积(mL)=" & WValoume & "},")
                    If shouldWrite Then
                        shouldNotify = True
                    End If
                End If

                If preProduct.MValoume <> Double.Parse(MValoume) Then
                    sbSQL.Append("[基准物容积(mL)]=" & MValoume & ",") '文本
                    sbLog.Append("{基准物容积(mL)=" & preProduct.MValoume & "}变为{基准物容积(mL)=" & MValoume & "},")
                    If shouldWrite Then
                        shouldNotify = True
                    End If
                End If

                If SystemData.Version <> Version_800_113 Then
                    If preProduct.MiddleJudgment <> MiddleJudgment Then
                        sbSQL.Append("中途判定模式=" & MiddleJudgment & ",") '文本
                        sbLog.Append("{中途判定模式=" & preProduct.MiddleJudgment & "}变为{中途判定模式=" & MiddleJudgment & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If

                    If preProduct.LeadPDAC <> LeadPDAC Then
                        sbSQL.Append("前导压DAC=" & LeadPDAC & ",") '文本
                        sbLog.Append("{前导压DAC=" & preProduct.LeadPDAC & "}变为{前导压DAC=" & LeadPDAC & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If

                    If preProduct.TestPDAC <> TestPDAC Then
                        sbSQL.Append("测试压DAC=" & TestPDAC & ",") '文本
                        sbLog.Append("{测试压DAC=" & preProduct.TestPDAC & "}变为{测试压DAC=" & TestPDAC & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                End If

            Else
                For i = 0 To 5
                    If Math.Abs(Double.Parse(preProduct.Times(i)) - Double.Parse(Times(i))) > TOLERANCE Then
                        sbSQL.Append(strTime800MS(i) & "=" & Times(i) & ",")
                        sbLog.Append("{" & strTime800MS(i) & "=" & preProduct.Times(i) & "}变为{" &
                                   strTime800(i) & "=" & Times(i) & "},")
                        ' TODO 提醒写入
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                Next

                For i = 0 To 3
                    If Math.Abs(Double.Parse(preProduct.Limits(i)) - Double.Parse(Limits(i))) > TOLERANCE Then
                        sbSQL.Append(strLimit800MS(i) & "=" & Limits(i) & ",")
                        sbLog.Append("{" & strLimit800MS(i) & "=" & preProduct.Limits(i) & "}变为{" &
                                   strLimit800MS(i) & "=" & Limits(i) & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                Next

                For i = 8 To 10
                    If Math.Abs(Double.Parse(preProduct.Limits(i)) - Double.Parse(Limits(i))) > TOLERANCE Then
                        sbSQL.Append(strLimit800(i) & "=" & Limits(i) & ",")
                        sbLog.Append("{" & strLimit800(i) & "=" & preProduct.Limits(i) & "}变为{" &
                                   strLimit800(i) & "=" & Limits(i) & "},")
                        If shouldWrite Then
                            shouldNotify = True
                        End If
                    End If
                Next
            End If

            If preProduct.LeakUnit <> LeakUnit Then
                sbSQL.Append("泄漏量单位='" & LeakUnit & "',") '文本
                sbLog.Append("{泄漏量单位=" & preProduct.LeakUnit & "}变为{泄漏量单位=" & LeakUnit & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.PressureUnit <> PressureUnit Then
                sbSQL.Append("压力单位='" & PressureUnit & "',") '文本
                sbLog.Append("{压力单位=" & preProduct.PressureUnit & "}变为{压力单位=" & PressureUnit & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.LeakDigit <> LeakDigit Then
                sbSQL.Append("泄漏量感度='" & LeakDigit & "',") '文本
                sbLog.Append("{泄漏量感度=" & preProduct.LeakDigit & "}变为{泄漏量感度=" & LeakDigit & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.TimerDigit <> TimerDigit Then
                sbSQL.Append("时间感度='" & TimerDigit & "',") '文本
                sbLog.Append("{时间感度=" & preProduct.TimerDigit & "}变为{时间感度=" & TimerDigit & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.UsbMode <> UsbMode Then
                sbSQL.Append("输出模式='" & UsbMode & "',") '文本
                sbLog.Append("{打印/U盘输出模式=" & preProduct.UsbMode & "}变为{打印/U盘输出模式=" & UsbMode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.RS232Mode <> RS232Mode Then
                sbSQL.Append("串口输出模式='" & RS232Mode & "',") '文本
                sbLog.Append("{串口输出模式=" & preProduct.RS232Mode & "}变为{串口输出模式=" & RS232Mode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.Cylinder <> Cylinder Then
                sbSQL.Append("气缸控制模式='" & Cylinder & "',") '文本
                sbLog.Append("{气缸控制模式=" & preProduct.Cylinder & "}变为{气缸控制模式=" & Cylinder & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.SV5Mode <> SV5Mode Then
                sbSQL.Append("SV5Mode='" & SV5Mode & "',") '文本
                sbLog.Append("{SV5输出模式=" & preProduct.SV5Mode & "}变为{SV5输出模式=" & SV5Mode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.Sv6Mode <> Sv6Mode Then
                sbSQL.Append("Sv6Mode='" & Sv6Mode & "',") '文本
                sbLog.Append("{SV6输出模式=" & preProduct.Sv6Mode & "}变为{SV6输出模式=" & Sv6Mode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.SV7Mode <> SV7Mode Then
                sbSQL.Append("SV7Mode='" & SV7Mode & "',") '文本
                sbLog.Append("{SV7输出模式=" & preProduct.SV7Mode & "}变为{SV7输出模式=" & SV7Mode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.SV8Mode <> SV8Mode Then
                sbSQL.Append("SV8Mode='" & SV8Mode & "',") '文本
                sbLog.Append("{SV8输出模式=" & preProduct.SV8Mode & "}变为{SV8输出模式=" & SV8Mode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.MasterMode <> MasterMode Then
                sbSQL.Append("Master模式=" & MasterMode & ",") '文本
                sbLog.Append("{Master模式=" & preProduct.MasterMode & "}变为{Master模式=" & MasterMode & "},")
                If shouldWrite Then
                    shouldNotify = True
                End If
            End If

            If preProduct.Temporary <> Temporary Then
                sbSQL.Append("临时存储=" & Temporary & ",") '文本
                sbLog.Append("{临时存储=" & preProduct.Temporary & "}变为{临时存储=" & Temporary & "},")
            End If


            If sbSQL.ToString = "" Then
                Return True
            End If

            Dim strCmd = Mid(sbSQL.ToString, 1, sbSQL.ToString.Length - 1)
            Dim strInfo = Mid(sbLog.ToString, 1, sbLog.ToString.Length - 1)

            strCmd = "UPDATE 产品管理 SET " & strCmd & " WHERE 产品号='" & Id & "'" '文本
            strInfo &= "}"

            log.WriteLogInfo("编辑产品", strInfo)

            Dim dbCmd As New OleDbCommand(strCmd, g_dbConn测试记录)

            Try
                If shouldNotify Then
                    g_udt800 = Me
                    Dim result = False

                    If IsMs Then
                        result = g_udtFL800Sync.WriteGroupSync(Chr(SerialPortServer.ENQ) & "2")
                    Else
                        If Version = Version_800_113 Then
                            result = g_udtFL800Sync.WriteGroupSync(Chr(SerialPortServer.ENQ) & "2")
                        Else
                            result = g_udtFL800Sync.WriteGroupSync(Chr(SerialPortServer.ENQ) & "9")
                        End If
                    End If

                    If Not result Then
                        MessageBox.Show("数据写入仪器失败")
                        Return False
                    End If
                    Messenger.Default.Send(Of String)("OK", "update_product")
                End If
                dbCmd.ExecuteNonQuery()
            Catch ex As Exception
                g_udt800 = preProduct
                log.WriteLogError("编辑产品:", "{更新失败原因=" & ex.Message & "SQL: " & strCmd & " " & strInfo & "}")
                Return False
            End Try

            Return True
        End Function



        Private _sv5List As ObservableCollection(Of Boolean)
        Private _sv6List As ObservableCollection(Of Boolean)
        Private _sv7List As ObservableCollection(Of Boolean)
        Private _sv8List As ObservableCollection(Of Boolean)


        Private _groupId As String
        Public Property GroupId() As String
            Get
                Return _groupId
            End Get
            Set(ByVal value As String)
                _groupId = value
            End Set
        End Property

        Private _times As ObservableCollection(Of String)
        Public Property Times() As ObservableCollection(Of String)
            Get
                Return _times
            End Get
            Set(ByVal value As ObservableCollection(Of String))
                _times = value
                RaisePropertyChanged()
            End Set
        End Property

        Private _limits As ObservableCollection(Of String)
        Public Property Limits() As ObservableCollection(Of String)
            Get
                Return _limits
            End Get
            Set(ByVal value As ObservableCollection(Of String))
                _limits = value
            End Set
        End Property

        Private _wValoume As String
        Public Property WValoume() As String
            Get
                Return _wValoume
            End Get
            Set(ByVal value As String)
                _wValoume = value
            End Set
        End Property

        Private _mValoume As String
        Public Property MValoume() As String
            Get
                Return _mValoume
            End Get
            Set(ByVal value As String)
                _mValoume = value
            End Set
        End Property

        Private _leakUnit As String
        Public Property LeakUnit() As String
            Get
                Return _leakUnit
            End Get
            Set(ByVal value As String)
                _leakUnit = value
            End Set
        End Property

        Private _pressureUnit As String
        Public Property PressureUnit() As String
            Get
                Return _pressureUnit
            End Get
            Set(ByVal value As String)
                _pressureUnit = value
            End Set
        End Property


        Private _leakDigit As String
        Public Property LeakDigit() As String
            Get
                Return _leakDigit
            End Get
            Set(ByVal value As String)
                _leakDigit = value
            End Set
        End Property

        Private _timerDigit As String
        Public Property TimerDigit() As String
            Get
                Return _timerDigit
            End Get
            Set(ByVal value As String)
                _timerDigit = value
                RaisePropertyChanged()
            End Set
        End Property

        Private _usbMode As String
        Public Property UsbMode() As String
            Get
                Return _usbMode
            End Get
            Set(ByVal value As String)
                _usbMode = value
            End Set
        End Property

        Private _rs232Mode As String
        Public Property RS232Mode() As String
            Get
                Return _rs232Mode
            End Get
            Set(ByVal value As String)
                _rs232Mode = value
            End Set
        End Property


        Private _cylinder As String
        Public Property Cylinder() As String
            Get
                Return _cylinder
            End Get
            Set(ByVal value As String)
                _cylinder = value
            End Set
        End Property

        Private _sv5Mode As String
        Public Property SV5Mode() As String
            Get
                Return _sv5Mode
            End Get
            Set(ByVal value As String)
                _sv5Mode = value
                For i = 0 To 7
                    _sv5List(i) = _sv5Mode(i) = "1"
                Next
            End Set
        End Property

        Private _sv6Mode As String
        Public Property Sv6Mode() As String
            Get
                Return _sv6Mode
            End Get
            Set(ByVal value As String)
                _sv6Mode = value
                For i = 0 To 7
                    _sv6List(i) = _sv6Mode(i) = "1"
                Next
            End Set
        End Property

        Private _sv7Mode As String
        Public Property SV7Mode() As String
            Get
                Return _sv7Mode
            End Get
            Set(ByVal value As String)
                _sv7Mode = value
                For i = 0 To 7
                    _sv7List(i) = _sv7Mode(i) = "1"
                Next
            End Set
        End Property

        Private _sv8Mode As String
        Public Property SV8Mode() As String
            Get
                Return _sv8Mode
            End Get
            Set(ByVal value As String)
                _sv8Mode = value
                For i = 0 To 7
                    _sv8List(i) = _sv8Mode(i) = "1"
                Next
            End Set
        End Property

        Private _masterMode As String
        Public Property MasterMode() As String
            Get
                Return _masterMode
            End Get
            Set(ByVal value As String)
                _masterMode = value
            End Set
        End Property

        Private _middleJudgment As Boolean
        Public Property MiddleJudgment() As Boolean
            Get
                Return _middleJudgment
            End Get
            Set(ByVal value As Boolean)
                _middleJudgment = value
            End Set
        End Property

        Private _leadPDAC As String
        Public Property LeadPDAC() As String
            Get
                Return _leadPDAC
            End Get
            Set(ByVal value As String)
                _leadPDAC = value
            End Set
        End Property

        Private _testPDAC As String
        Public Property TestPDAC() As String
            Get
                Return _testPDAC
            End Get
            Set(ByVal value As String)
                _testPDAC = value
            End Set
        End Property

        Public Property Sv5List As ObservableCollection(Of Boolean)
            Get
                Return _sv5List
            End Get
            Set(value As ObservableCollection(Of Boolean))
                _sv5List = value
            End Set
        End Property

        Public Property Sv6List As ObservableCollection(Of Boolean)
            Get
                Return _sv6List
            End Get
            Set(value As ObservableCollection(Of Boolean))
                _sv6List = value
            End Set
        End Property

        Public Property Sv7List As ObservableCollection(Of Boolean)
            Get
                Return _sv7List
            End Get
            Set(value As ObservableCollection(Of Boolean))
                _sv7List = value
            End Set
        End Property

        Public Property Sv8List As ObservableCollection(Of Boolean)
            Get
                Return _sv8List
            End Get
            Set(value As ObservableCollection(Of Boolean))
                _sv8List = value
            End Set
        End Property

        Public Property IsMs As Boolean
            Get
                Return _isMs
            End Get
            Set(value As Boolean)
                _isMs = value
            End Set
        End Property
    End Class
End Namespace